package com.shigu.user.action;

import com.shigu.MallResponse;
import com.shigu.TokenRequest;
import com.shigu.exceptions.WapException;
import com.shigu.task.bo.SubmitDataRequest;
import com.shigu.task.bo.SubmitTaskBo;
import com.shigu.task.service.MallTaskService;
import com.shigu.task.vo.ListResponse;
import com.shigu.task.vo.TaskVo;
import com.shigu.user.service.MallTokenService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/*
 * create by  过儿  2019-09-20
 *
 */
@RequestMapping("/wapsjson")
@Controller
public class UserTaskController {

    @Resource(name = "mallTaskServiceImpl")
    MallTaskService mallTaskService;

    @Autowired
    MallTokenService mallTokenService;

    @RequestMapping("getTodoTaskList")
    @ResponseBody
    public ListResponse<TaskVo> todoTaskList(TokenRequest request) {
        Long mallUserId = null;
        if (StringUtils.isNotBlank(request.getToken())) {
            mallUserId = mallTokenService.selUserIdByToken(request.getToken());
        }
        return mallTaskService.todoTaskList(mallUserId);
    }

    @RequestMapping("getHaveTodoTaskList")
    @ResponseBody
    public ListResponse<TaskVo> haveDoTaskList(TokenRequest request) {
        Long mallUserId = null;
        if (StringUtils.isNotBlank(request.getToken())) {
            mallUserId = mallTokenService.selUserIdByToken(request.getToken());
        }
        return mallTaskService.haveDoTaskList(mallUserId);
    }

    @RequestMapping("submitTaskData")
    @ResponseBody
    public MallResponse submitTaskData(@RequestBody SubmitDataRequest request) throws WapException {
        MallResponse response = new MallResponse();
        if(StringUtils.isBlank(request.getToken())) {
            response.setApi(false);
            response.setMsg("用户请登录");
            response.setSuccess(false);
            return response;
        }
        Long id = mallTokenService.selUserIdByToken(request.getToken());
        if(id == null){
            response.setApi(false);
            response.setMsg("登录信息失效");
            response.setSuccess(false);
            return response;
        }

        SubmitTaskBo bo = new SubmitTaskBo();
        BeanUtils.copyProperties(request,bo);
        mallTaskService.submitTaskData(bo,id,Long.valueOf(request.getTaskId()));

        return response.success();
    }
}
